/**
 * Copyright (c) 2010 David Hamrick, Guy Kopsombut, Emily Ross 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//#import "User.h"

#import "sunna_iphone_cabdriverAppDelegate.h"
#import "RootVC.h"
#import "GPSInfoVC.h"

#import "FarePickerVC.h"
#import "CLController.h"
#import "CurrentFareVC.h"

#import "Passenger.h"

@implementation RootVC

@synthesize rootVCState,rootVCHasPassengerState,rootVCNoPassengerState;

enum RootVCTableRows
{
	RootVCTransmitRow,
	RootVCGPSRow,
	RootVCFaresRow,
	RootVCNumRows
};

/**
 * Sets up the root view controller
 * Adds a title, transmit switch, and location controller
 */
- (id)initWithStyle:(UITableViewStyle)style 
{
   if (self = [super initWithStyle:style]) 
	{
		self.title = @"Cab Driver";
		transmitSwitch = [[UISwitch alloc] init];
		[transmitSwitch addTarget:self action:@selector(transmitSwitchChanged) forControlEvents:UIControlEventValueChanged];
		transmitSwitch.on = NO;
		
		CLController* location = [[CLController alloc] init];
		[location.locationManager startUpdatingLocation];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hasLocation) name:@"NewGPS" object:nil];
	
		rootVCHasPassengerState = [[RootVCHasPassengerState alloc] initWithRootVC:self]; 
		rootVCNoPassengerState = [[RootVCNoPassengerState alloc] initWithRootVC:self];
		rootVCState = rootVCNoPassengerState;
		
	}
	return self;
}


/**
 * Animates the entrace of a view
 */
- (void)viewWillAppear:(BOOL)animated 
{
	Passenger* passenger = [[[UIApplication sharedApplication] delegate] assignedPassenger];
	if(passenger != nil)
		rootVCState = rootVCHasPassengerState;
	else
		rootVCState = rootVCNoPassengerState;
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(assignedPassenger) name:@"AssignedPassenger" object:nil];
	
	[super viewWillAppear:animated];
	[self.tableView reloadData];
}

/**
 * Returns the number of rows in our section
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	return RootVCNumRows;
}


/**
 * Defines what a cell is and the cells in our rows.
 *
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString *CellIdentifier = @"Cell";

	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
	}
	switch([indexPath row])
	{
		case RootVCTransmitRow:
			cell.textLabel.text = @"Transmit";
			cell.accessoryType = UITableViewCellAccessoryNone;
			if(cell.accessoryView == nil)
				cell.accessoryView = transmitSwitch;
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
			break;
		case RootVCGPSRow:
			cell.textLabel.text = @"GPS Information";
			//if([[User Inst] locationAvailable])
				cell.detailTextLabel.text = @"Has Signal";
			//else 
			//	cell.detailTextLabel.text = @"No Signal";
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			cell.accessoryView = nil;
			cell.selectionStyle = UITableViewCellSelectionStyleBlue;
			break;
		case RootVCFaresRow:
			cell.textLabel.text = [rootVCState textForFaresCell];
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			cell.selectionStyle = UITableViewCellSelectionStyleBlue;
			break;
	}
	return cell;
}



/**
 * Assigns action depending on area of touch on main screen 
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	switch([indexPath row])
	{
		case RootVCTransmitRow:
			break;
		case RootVCGPSRow:
			[self showGPSInfoVC];
			break;
		case RootVCFaresRow:
			[rootVCState selectedFaresRow];
		default:
			break;
	}
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/*
 * Releases memory
 */
- (void)dealloc 
{
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"NewGPS" object:nil];
   [super dealloc];
}

- (void)showGPSInfoVC
{
	//if(![[User Inst] locationAvailable])
	//	return;
	GPSInfoVC* vc = [[[GPSInfoVC alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
	[self.navigationController pushViewController:vc animated:YES];
}

- (void)showFarePickerVC
{
	FarePickerVC* vc = [[[FarePickerVC alloc] initWithStyle:UITableViewStylePlain] autorelease];
	[self.navigationController pushViewController:vc animated:YES];
}

- (void)showPassengerVC
{
	CurrentFareVC* vc = [[[CurrentFareVC alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
	[self.navigationController pushViewController:vc animated:YES];
}

- (void)transmitSwitchChanged
{
	//[User Inst].transmit = transmitSwitch.on;
}

- (void)assignedPassenger
{

	Passenger* passenger = [(sunna_iphone_cabdriverAppDelegate*)[[UIApplication sharedApplication] delegate] assignedPassenger];
	
	if(passenger != nil)
		rootVCState = rootVCHasPassengerState;
	else
		rootVCState = rootVCNoPassengerState;
	[self.tableView reloadData];
}



- (void)hasLocation
{
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"NewGPS" object:nil];
	[self.tableView reloadData];
}


@end


@implementation RootVCState

@synthesize _rootVC;

- (id)initWithRootVC:(RootVC*)rootVC
{
	if(self = [super init])
	{
		self._rootVC = rootVC;
	}
	return self;
}
- (NSString*)textForFaresCell {return nil;}  //Intentionally empty
- (void)selectedFaresRow {}       //Intentionally empty

@end

@implementation RootVCHasPassengerState

- (NSString*)textForFaresCell
{
	return @"Current Fare";
}
- (void)selectedFaresRow
{
	[_rootVC showPassengerVC];
}

@end

@implementation RootVCNoPassengerState

- (NSString*)textForFaresCell
{
	return @"Available Fare";
}

- (void)selectedFaresRow
{
	[_rootVC showFarePickerVC];
}

@end





